package chuishop.yiqimaicai.controller;

import chuishop.yiqimaicai.pojo.Employee;
import chuishop.yiqimaicai.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("findAllEmployee")
    @ResponseBody
    //public List<Employee> findAllEmployee(){//无分页
    public PageInfo<Employee> findAllEmployee(Integer pageNo, Integer pageSize){
        List<Employee> list=employeeService.findAllEmployee();
//        for(int i=0;i<list.size();i++) {
//            System.out.println(list.get(i).getId()+list.get(i).getName());
//        }
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null) {
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Employee> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @RequestMapping("addEmployee")
    @ResponseBody
    public Map<Object,Object> addEmployee(@RequestBody Employee employee){
        System.out.println(employee.getName()+employee.getPassword());
        Map<Object,Object> result=new HashMap<>();
        boolean flag=employeeService.addEmployee(employee);
        if (flag){
            result.put("status",200);
            result.put("msg","添加成功");
        }else{
            result.put("status",500);
            result.put("msg","添加失败");
        }
        return result;
    }

    @RequestMapping("deleteEmployee")
    @ResponseBody
    public Map<Object,Object> deleteEmployee(String ids){
        System.out.println("deleteEmployee的ids为:"+ids);
        String idstr[]=ids.split(",");

        Map<Object,Object> result=new HashMap<>();
        boolean flag=employeeService.deleteEmployee(idstr);
        if(flag){
            result.put("status","200");
            result.put("msg","添加成功");
        }else {
            result.put("status","500");
            result.put("msg","添加失败");
        }
        return result;
    }

    @RequestMapping("updateEmployee")
    @ResponseBody
    public Map<Object,Object> updateEmployee(@RequestBody Employee employee){
        Map<Object,Object> result=new HashMap<>();
        boolean flag=employeeService.updateEmployee(employee);
        if(flag){
            result.put("status","200");
            result.put("msg","更新成功");
        }else {
            result.put("status","500");
            result.put("msg","更新失败");
        }
        return result;
    }

    @RequestMapping("searchEmployeeByname")
    @ResponseBody
    public List<Employee> searchEmployeeByname(String name1){//无分页
        System.out.println("searchEmployeeByname的name1为:"+name1);
        List<Employee> list=employeeService.searchEmployeeByname(name1);
        for(int i=0;i<list.size();i++) {
            System.out.println(list.get(i).getId()+list.get(i).getName());
        }
        return list;
    }



    @RequestMapping("login")
    @ResponseBody
    public Map<Object,Object> loginEmployee(String idpassword, @ModelAttribute Employee employee, HttpSession session){
        //System.out.println("login的idpassword为:"+idpassword);
        String idpasswordstr[]=idpassword.split(",");
        /*for(String str:idpasswordstr){
            System.out.println(str);
        }*/
        Integer id=Integer.parseInt(idpasswordstr[0]);
        String password=idpasswordstr[1];
        //System.out.println("login的id为："+id+","+"password的为："+password);

        Map<Object,Object> result=new HashMap<>();
        boolean flag=false;
        //调用业务层登录的方法
        Employee employee1=employeeService.login(id,password);
       //System.out.println("controller层的Login的employee1的id和password分别为："+employee1.getId()+','+employee1.getPassword());
//
        if(employee1!=null){
            System.out.println(employee1.getName());
            session.setAttribute("employee",employee1);
            flag=true;
        }else{
            flag=false;
        }//else的

        if(flag){
            result.put("status","200");
            result.put("msg","登录成功");
        }else {
            result.put("status","500");
            result.put("msg","登录失败");
        }
        return result;
    }

    @RequestMapping("findEmployeeBylimits")
    @ResponseBody
    public List<Employee> findEmployeeBylimits(int limits){
        List<Employee> list=employeeService.findEmplyeeBylimits(limits);
        return list;

    }







}
